package com.magicborrow.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.beans.BaseBean;
import com.magicborrow.utils.MD5;
import com.magicborrow.utils.VolleyTool;
import com.magicborrow.views.LoadingDialog;

import java.util.HashMap;

public class ForgotPasswordActivity extends BaseActivity {

    private ImageView imbBack;
    private TextView tvPhone;
    private TextView tvConfirm;
    private String phone;
    private String code;
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgot_password);
        imbBack = (ImageView) findViewById(R.id.imb_back);
        tvPhone = (TextView) findViewById(R.id.tv_phone);
        tvConfirm = (TextView) findViewById(R.id.tv_confirm);
        initData();
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = getEditPassword().getText().toString();
                if(TextUtils.isEmpty(password)||password.length()<6){
                    showShortMsg("请输入6到12位的密码");
                    return;
                }
                loadingDialog.show();
                HashMap<String,String> params = new HashMap<>();
                params.put("mobile",phone);
                params.put("password", MD5.getMD5(MD5.getMD5(password)));
                params.put("verifyToken",code);
                VolleyTool.get(Constants.URL_FOTGET_PASSWORD, params, new VolleyTool.HTTPListener() {
                    @Override
                    public <T> void onResponse(T t, int requestCode) {
                        BaseBean bean = (BaseBean) t;
                        if(bean.getCode()>=0){
                            showShortMsg("密码修改成功");
                            finish();
                        }else {
                            showShortMsg(bean.getMessage());
                        }
                        loadingDialog.dismiss();
                    }

                    @Override
                    public void onErrorResponse(VolleyError error, int requestCode) {

                    }
                },0, BaseBean.class);

            }
        });
    }
    private void initData() {
        loadingDialog = new LoadingDialog(this);
        Bundle bundle = this.getIntent().getExtras();
        phone = bundle.getString("phone");
        code = bundle.getString("checkCode");
        tvPhone.setText(phone);

    }

    private EditText getEditPassword(){
        return (EditText) findViewById(R.id.edit_password);
    }
}
